//
//  PPSheetView.m
//  pickpic
//
//  Created by 胡文广 on 2017/3/30.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPSheetView.h"
#define kItemH 44
@interface PPSheetView()
@property (nonatomic, strong) PPSheetContentView *contentView;
@end
@implementation PPSheetView
- (id)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]){
        [self initContent];
    }
    return self;
}
- (void)initContent{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    //alpha 0.0  白色   alpha 1 ：黑色   alpha 0～1 ：遮罩颜色，逐渐
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]];
    WEAKSELF;
    self.contentView.clickBtn = ^(PPSheetContentBtn *btn){
        if ([weakSelf.delegate respondsToSelector:@selector(sheetView:didSelectedIndex:)]) {
            [weakSelf.delegate sheetView:weakSelf didSelectedIndex:btn.tag];
        }
    };
    [self addSubview:self.contentView];
}
- (UIView *)contentView{

    if (!_contentView) {
        _contentView = [[PPSheetContentView alloc] init];
    }
    return _contentView;
}
- (void)setItems:(NSArray *)items{
    _items = items;
    if (_items.count > 0) {
        self.contentView.frame = CGRectMake(0, 0,self.width,kItemH*items.count);
        for (NSString *title in items) {
          [self.contentView addSheetItemTitle:title];
        }
    }
}

- (void)showInView:(UIView *)view
{
    if (!view){ return; }
    
    [view addSubview:self];
    [view addSubview:self.contentView];
    
    [_contentView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.items.count * kItemH)];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
        [_contentView setFrame:CGRectMake(0, SCREEN_HEIGHT - self.items.count * kItemH, SCREEN_WIDTH, self.items.count * kItemH)];
    } completion:nil];
}

//移除从上向底部弹下去的UIView（包含遮罩）
- (void)disMissView
{
    [self.contentView setFrame:CGRectMake(0, SCREEN_HEIGHT - self.items.count * kItemH, SCREEN_WIDTH, self.items.count * kItemH)];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.alpha = 0.0;
                         [self.contentView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.items.count * kItemH)];
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         [_contentView removeFromSuperview];
                     }];
    
}
@end


@implementation PPSheetContentView : UIView
- (PPSheetContentBtn *)addSheetItemTitle:(NSString *)title
{
    PPSheetContentBtn *btn = [[PPSheetContentBtn alloc] init];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = PPFONTSIZE(14.0f);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = WHITE;
    [self addSubview:btn];
    
    return btn;
}
#pragma mark 点击事件
- (void)buttonClick:(PPSheetContentBtn *)btn
{
    if (self.clickBtn) {
        self.clickBtn(btn);
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 按钮的frame数据
    CGFloat buttonH = self.frame.size.height / self.subviews.count;
    CGFloat buttonW = self.frame.size.width;
    CGFloat buttonX = 0;
    for (int index = 0; index<self.subviews.count; index++) {
        PPSheetContentBtn *button = self.subviews[index];
        CGFloat buttonY = index * buttonH;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        button.tag = index;
    }
}
@end



@implementation PPSheetContentBtn : UIButton

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 0.5);  //线宽
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetRGBStrokeColor(context, 204.0 / 255.0, 204.0 / 255.0, 204.0 / 255.0, 1.0);  //线的颜色
    CGContextBeginPath(context);
    
    CGContextMoveToPoint(context, 0, self.frame.size.height);  //起点坐标
    CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);   //终点坐标
    
    CGContextStrokePath(context);
}
@end

